#include "colorwidget.h"
#include "ui_colorwidget.h"

#include <QPainter>
#include <QBitmap>

ColorWidget::ColorWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ColorWidget)
{
    ui->setupUi(this);

    initUI();
}

QColor ColorWidget::GetColor()
{
    return color;
}

unsigned short ColorWidget::GetSize()
{
    return Size;
}


void ColorWidget::initUI() {
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlags(Qt::FramelessWindowHint | windowFlags() | Qt::Tool);
    setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint);

    QBitmap bmp(this->size());
    bmp.fill();
    QPainter p(&bmp);
    p.setPen(Qt::NoPen);
    p.setBrush(Qt::black);
    p.drawRoundedRect(bmp.rect(),5,5);
    setMask(bmp);

    ui->color_black->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_black_default.png);");

    //signe
    connect(ui->color_black, SIGNAL(clicked()), this, SLOT(onClickedBlackColor()));
    connect(ui->color_white, SIGNAL(clicked()), this, SLOT(onClickedWhiteColor()));
    connect(ui->color_blue, SIGNAL(clicked()), this, SLOT(onClickedBlueColor()));
    connect(ui->color_green, SIGNAL(clicked()), this, SLOT(onClickedGreenColor()));
    connect(ui->color_yellow, SIGNAL(clicked()), this, SLOT(onClickedYellowColor()));
    connect(ui->color_orange, SIGNAL(clicked()), this, SLOT(onClickedOrangleColor()));
    connect(ui->color_red, SIGNAL(clicked()), this, SLOT(onClickedRedColor()));
    connect(ui->color_purple, SIGNAL(clicked()), this, SLOT(onClickedPurpleColor()));

    connect(ui->size_1, SIGNAL(clicked()), this, SLOT(onButtonSize1Clicked()));
    connect(ui->size_2, SIGNAL(clicked()), this, SLOT(onButtonSize2Clicked()));
    connect(ui->size_3, SIGNAL(clicked()), this, SLOT(onButtonSize3Clicked()));
    connect(ui->size_4, SIGNAL(clicked()), this, SLOT(onButtonSize4Clicked()));
}

void ColorWidget::onClickedBlackColor() {
    ui->color_black->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Black;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }
    emit changeColor();
}

void ColorWidget::onClickedWhiteColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_White;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedBlueColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Blue;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedGreenColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Green;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedYellowColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;border-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Yellow;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedOrangleColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Orange;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedRedColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #ff40ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Red;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onClickedPurpleColor() {
    ui->color_black->setStyleSheet("border:2px solid #000000;background-image: url(:/new/icon/color/color_black_default.png);");
    ui->color_white->setStyleSheet("border:2px solid #ffffff;border-image: url(:/new/icon/color/color_white_default.png);");
    ui->color_blue->setStyleSheet("border:2px solid #0000ff;background-image: url(:/new/icon/color/color_blue_default.png);");
    ui->color_green->setStyleSheet("border:2px solid #00f900;background-image: url(:/new/icon/color/color_green_default.png);");
    ui->color_yellow->setStyleSheet("border:2px solid #fffc00;background-image: url(:/new/icon/color/color_yellow_default.png);");
    ui->color_orange->setStyleSheet("border:2px solid #ff8517;background-image: url(:/new/icon/color/color_orange_default.png);");
    ui->color_red->setStyleSheet("border:2px solid #ff0000;background-image: url(:/new/icon/color/color_red_default.png);");
    ui->color_purple->setStyleSheet("border:2px solid #4381ff;background-image: url(:/new/icon/color/color_purple_default.png);");

    color = COLORS::COLOR_Purple;
    if (isSelectNitePen) {
        color.setAlpha(80);
    }else {
        color.setAlpha(255);
    }

    emit changeColor();
}

void ColorWidget::onButtonSize1Clicked() {
    qDebug("on_Button_Size1_clicked");
    Size = SIZES::SizeRate*1;
    ui->size_1->setStyleSheet("border-image: url(:/new/icon/color/size_click_1.png);");
    ui->size_2->setStyleSheet("border-image: url(:/new/icon/color/size_default_2.png);");
    ui->size_3->setStyleSheet("border-image: url(:/new/icon/color/size_default_3.png);");
    ui->size_4->setStyleSheet("border-image: url(:/new/icon/color/size_default_4.png);");
    emit changeSize();
}

void ColorWidget::onButtonSize2Clicked() {
    qDebug("on_Button_Size2_clicked");
    Size = SIZES::SizeRate*2;
    ui->size_1->setStyleSheet("border-image: url(:/new/icon/color/size_default_1.png);");
    ui->size_2->setStyleSheet("border-image: url(:/new/icon/color/size_click_2.png);");
    ui->size_3->setStyleSheet("border-image: url(:/new/icon/color/size_default_3.png);");
    ui->size_4->setStyleSheet("border-image: url(:/new/icon/color/size_default_4.png);");
    emit changeSize();
}

void ColorWidget::onButtonSize3Clicked() {
    qDebug("on_Button_Size3_clicked");
    Size = SIZES::SizeRate*3;
    ui->size_1->setStyleSheet("border-image: url(:/new/icon/color/size_default_1.png);");
    ui->size_2->setStyleSheet("border-image: url(:/new/icon/color/size_default_2.png);");
    ui->size_3->setStyleSheet("border-image: url(:/new/icon/color/size_click_3.png);");
    ui->size_4->setStyleSheet("border-image: url(:/new/icon/color/size_default_4.png);");
    emit changeSize();
}

void ColorWidget::onButtonSize4Clicked() {
    qDebug("on_Button_Size4_clicked");
    Size = SIZES::SizeRate*4;
    ui->size_1->setStyleSheet("border-image: url(:/new/icon/color/size_default_1.png);");
    ui->size_2->setStyleSheet("border-image: url(:/new/icon/color/size_default_2.png);");
    ui->size_3->setStyleSheet("border-image: url(:/new/icon/color/size_default_3.png);");
    ui->size_4->setStyleSheet("border-image: url(:/new/icon/color/size_click_4.png);");
    emit changeSize();
}

ColorWidget::~ColorWidget()
{
    delete ui;
}
